package org.benetech.event;

import java.io.File;
import java.io.IOException;

import org.benetech.exception.WrappedException;
import org.benetech.util.FileUtils;


/**
 * Logs events to a specific file. May not be as efficient as the specialized logging classes.
 * @author Reuben Firmin
 */
public final class FileLoggingEventListener implements EventListener {

	private File file;

	/**
	 * Initialize with the file to log to.
	 * @param file The file to log to
	 */
	public FileLoggingEventListener(final File file) {
		this.file = file;
	}

	/**
	 * {@inheritDoc}
	 */
	public void error(final String error) {
		try {
			FileUtils.appendToFile(file, error.getBytes(), "\n".getBytes());
		} catch (IOException e) {
			throw new WrappedException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public void message(final String message) {
		try {
			FileUtils.appendToFile(file, message.getBytes(), "\n".getBytes());
		} catch (IOException e) {
			throw new WrappedException(e);
		}
	}

}
